/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Basic;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Alejandro
 */
@Entity
@Table(name = "RS_PURCHASES")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Purchase.findAll", query = "SELECT r FROM Purchase r"),
    @NamedQuery(name = "Purchase.findByIdPurchase", query = "SELECT r FROM Purchase r WHERE r.idPurchase = :idPurchase"),
    @NamedQuery(name = "Purchase.findByUser", query = "SELECT r FROM Purchase r WHERE r.user = :user ORDER BY r.purchaseDate DESC"),
    @NamedQuery(name = "Purchase.findByDatetime", query = "SELECT r FROM Purchase r WHERE r.purchaseDate = :purchaseDate")})
public class Purchase implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue
    @Column(name = "ID_PURCHASE")
    private Integer idPurchase;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "PURCHASE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date purchaseDate;
    
    @ManyToOne
    @NotNull
    @JoinColumn(name="ID_USER", nullable = false)
    private User user;
    
    @OneToMany (mappedBy="purchase")
    private List<ItemPurchased> itemPurchase;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "STATUS")
    private Integer status;
    
    public Purchase() {
        this.status=1;
    }

    public Purchase(Integer idPurchase) {
        this.idPurchase = idPurchase;
    }

    public Purchase(Integer idPurchase, User user) {
        this.idPurchase = idPurchase;
        this.purchaseDate = new Date();
        this.status=1;
        this.user = user;
    }

    public Integer getIdPurchase() {
        return idPurchase;
    }

    public void setIdPurchase(Integer idPurchase) {
        this.idPurchase = idPurchase;
    }

//    public String formatPurchaseDate(){
//        SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm");
//        return sdf.format(purchaseDate);
//    }
    
    public Date getPurchaseDate() {
        return purchaseDate;
    }

    public void setPurchaseDate(Date purchaseDate) {
        this.purchaseDate = purchaseDate;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public List<ItemPurchased> getItemPurchase() {
        return itemPurchase;
    }

    public void setItemPurchase(List<ItemPurchased> itemPurchase) {
        this.itemPurchase = itemPurchase;
    }



    
    

   

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idPurchase != null ? idPurchase.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Purchase)) {
            return false;
        }
        Purchase other = (Purchase) object;
        if ((this.idPurchase == null && other.idPurchase != null) || (this.idPurchase != null && !this.idPurchase.equals(other.idPurchase))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Purchase[ idPurchase=" + idPurchase + " ]";
    }
    
    public Double total(){
        Double res=0.0;
        for(ItemPurchased p:itemPurchase){
            res+=p.getPrice()*p.getQuantity();
        }
        return Math.rint((res)*100)/100;
    }
    
    public Integer numberItemsPurchased(){
        return itemPurchase.size();
    }
    
    public Integer quantityItemsPurchased(){
         Integer res=0;
        for(ItemPurchased p:itemPurchase){
            res+=p.getQuantity();
        }
        return res;
    }
    
}
